package top.txl.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.txl.Dto.SetmealDto;
import top.txl.entity.PageBean;
import top.txl.entity.SetMeal;
import top.txl.entity.SetmealDish;
import top.txl.exception.CustomerException;
import top.txl.mapper.SetmealDishMapper;
import top.txl.mapper.SetmealMapper;
import top.txl.service.SetmealService;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Override
    @Transactional
    public void save(SetmealDto setmealDto) {
        setmealMapper.saveSetmeal(setmealDto);
        Integer setmealId = setmealDto.getId();
        List<SetmealDish> smdLists = setmealDto.getSmdLists();

        smdLists = smdLists.stream().map(item ->{
            item.setSetmealId(setmealId);
            item.setDishId(item.getId());
            item.setCreateUser(setmealDto.getCreateUser());
            item.setUpdateUser(setmealDto.getUpdateUser());
            item.setCreateTime(LocalDateTime.now());
            item.setUpdateTime(LocalDateTime.now());
            item.setIsDeleted(0);
            return item;
        }).collect(Collectors.toList());

        setmealDishMapper.saveSetmealDish(smdLists);
    }

    @Override
    public PageBean lists(Integer page, Integer pageSize, String keyWords) {
        PageHelper.startPage(page,pageSize);

        List<SetMeal> lists = setmealMapper.getsetmeals(keyWords);

        Page p = (Page<SetMeal>) lists;
        PageBean pageBean = new PageBean(p.getTotal(),p.getResult());
        return pageBean;
    }

    @Override
    @Transactional
    public SetMeal detail(Integer id) {
        SetMeal setMeal =  setmealMapper.getDetail(id);
        List<SetmealDish> lists = setmealDishMapper.getListsById(id);
        setMeal.setSmdLists(lists);
        return setMeal;
    }

    @Override
    @Transactional
    public void update(SetmealDto setmealDto) {

        // 判断是否存在ids,status，假如存在，那么是修改status
        if(!setmealDto.getIds().equals(null) && setmealDto.getIds().size() > 0){
            List<Integer> ids = setmealDto.getIds();
            Integer status = setmealDto.getStatus();
            setmealMapper.updateStatus(ids,status);
        }else{
            setmealMapper.update(setmealDto);
            Integer setmealId = setmealDto.getId();
            List<Integer> ids = new ArrayList<>();
            ids.add(setmealId);
            // 先删除
            setmealDishMapper.deleteSetmealDish(ids);

            List<SetmealDish> smdLists = setmealDto.getSmdLists();
            smdLists = smdLists.stream().map(item ->{
                item.setSetmealId(setmealId);
                item.setDishId(item.getId());
                item.setCreateUser(setmealDto.getUpdateUser());
                item.setUpdateUser(setmealDto.getUpdateUser());
                item.setCreateTime(LocalDateTime.now());
                item.setUpdateTime(LocalDateTime.now());
                return item;
            }).collect(Collectors.toList());;

            // 再添加
            setmealDishMapper.saveSetmealDish(smdLists);
        }
    }

    @Override
    public void deletes(List<Integer> ids) {
        if(!ids.equals(null) && ids.size() <= 0){
            throw new CustomerException("参数错误，缺少ids集合");
        }
        // 删除套餐
        setmealMapper.deletes(ids);

        // 删除套餐下的关联菜品
        setmealDishMapper.deleteSetmealDish(ids);
    }

    @Override
    public List<SetmealDto> frontLists(Integer categoryId) {
        List<SetMeal> setmeals = setmealMapper.frontSetmealLists(categoryId);
        List<SetmealDto> dtos = setmeals.stream().map(item -> {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(item, setmealDto);

            // 获取套餐id
            Integer sId = item.getId();
            // 根据套餐id 获取菜品数据
            List<SetmealDish> dishs = setmealDishMapper.getListsById(sId);
            setmealDto.setSmdLists(dishs);
            return setmealDto;
        }).collect(Collectors.toList());

        return dtos;
    }
}
